package com.sws.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSONObject;
import com.sws.exception.ParseXmlException;
import com.sws.model.ActivityModel;

public class ActivityRecordParse {
	
	public static void getXlsInof(String filePath, int sheet) {
		try {
			FileInputStream fis = new FileInputStream(new File(filePath));
			HSSFWorkbook workbook = new HSSFWorkbook(fis);
			HSSFSheet childSheet = workbook.getSheetAt(sheet);
			String info = childSheet.getRow(1).getCell(0).getStringCellValue().replace(" ", "");
			String gradeMajorClazz = info.substring(info.indexOf("班级：")+3, info.indexOf("班级人数："));
			int grade = Integer.parseInt(gradeMajorClazz.substring(0, 3));
			String major = gradeMajorClazz.substring(4, gradeMajorClazz.length()-2);
			int clazz = Integer.parseInt(gradeMajorClazz.substring(gradeMajorClazz.length()-2, gradeMajorClazz.length()-1));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<ActivityModel> xlsParse(String filePath, int sheet) {
		List<ActivityModel> list = new ArrayList<ActivityModel>();
		try {
			FileInputStream fis = new FileInputStream(new File(filePath));
			HSSFWorkbook workbook = new HSSFWorkbook(fis);
			HSSFSheet childSheet = workbook.getSheetAt(sheet);
			
			int lastRowNum = childSheet.getLastRowNum();
			int lastCellNum = childSheet.getRow(2).getLastCellNum();
			
			String info = childSheet.getRow(1).getCell(0).getStringCellValue().replace(" ", "");
			String [] yearMonth = info.substring(info.indexOf("月份：")+3, info.indexOf("上报时间：")).split("-");
			int year = Integer.parseInt(yearMonth[0]);
			int month = Integer.parseInt(yearMonth[1]);
			
			HSSFRow row = null;
			HSSFCell cell = null;
			
			for (int i = 3; i < lastRowNum; i++) {
				row = childSheet.getRow(i);
				int userNo = Integer.parseInt(getValue(row.getCell(1)));
				for (int j = 4; j < lastCellNum-1; j++) {
					cell = row.getCell(j);
					String value = getValue(cell);
					if (value != null && !value.isEmpty()) {
						ActivityModel record = new ActivityModel();
						record.setNo(userNo);
						record.setName(value.replaceAll(" ", ""));
						record.setMonth(month);
						record.setWeeks(j/4);
						record.setYear(year);
						list.add(record);
						System.out.println(JSONObject.toJSONString(record));
					}
				}
			}
			
			//System.out.println(last + " " + childSheet.getRow(0).getCell(0).getStringCellValue());
			
		} catch (Exception e) {
			// TODO: handle exception
			throw new ParseXmlException("文件解析错误，请选择正确文件");
		}
		
		return list;
	}
	
	private static String getValue(HSSFCell cell) {
		try {
			return cell.getStringCellValue();
		} catch (Exception e) {
		}
		try {
			return String.valueOf((int)cell.getNumericCellValue());
		} catch (Exception e) {
		}
		try {
			return String.valueOf(cell.getBooleanCellValue());
		} catch (Exception e) {
		}
		return null;
	}
}
